import win32gui
import win32con
import win32clipboard as w


# 通过昵称发送QQ消息
class SendQQMsg(object):

    def get_text(self):
        """获取剪贴板文本"""
        w.OpenClipboard()
        d = w.GetClipboardData(win32con.CF_UNICODETEXT)
        w.CloseClipboard()
        return d

    def __set_text(self, content):
        """设置剪贴板文本"""
        w.OpenClipboard()
        w.EmptyClipboard()
        w.SetClipboardData(win32con.CF_UNICODETEXT, content)
        w.CloseClipboard()

    # qq备注， 要发送的消息
    def send_qq(self, qq_nike, msg):
        """发送qq消息
        to_who：qq消息接收人
        msg：需要发送的消息
        """
        # 将消息写到剪贴板
        self.__set_text(msg)
        # 获取qq窗口句柄
        qq = win32gui.FindWindow(None, qq_nike)
        # 投递剪贴板消息到QQ窗体
        win32gui.SendMessage(qq, 258, 22, 2080193)
        win32gui.SendMessage(qq, 770, 0, 0)
        # 模拟按下回车键
        win32gui.SendMessage(qq, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
        win32gui.SendMessage(qq, win32con.WM_KEYUP, win32con.VK_RETURN, 0)


if __name__ == '__main__':
    sendQQMsg = SendQQMsg()
    sendQQMsg.send_qq("Light_Rain", "你好")
